package cn.tedu.ykmall.user.webapi.controller;

import cn.tedu.ykmall.common.web.JsonResult;
import cn.tedu.ykmall.pojo.userClient.dto.UserInsertDTO;
import cn.tedu.ykmall.pojo.userClient.dto.UserLoginDTO;
import cn.tedu.ykmall.pojo.userClient.vo.UserInfoVO;
import cn.tedu.ykmall.pojo.userClient.vo.UserPasswordVO;
import cn.tedu.ykmall.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/user", produces = "application/json; charset=utf-8")
public class UserController {
    public UserController() {
        System.out.println("UserController.UserController()");
    }

    @Autowired
    private IUserService iUserService;

    @PostMapping(value="/reg",produces = "application/json;charset=utf-8")
    public JsonResult<Void> reg(@RequestBody UserInsertDTO userInsertDTO){
        iUserService.addNew(userInsertDTO);
        return JsonResult.ok();
    }

    @PostMapping(value = "/login")
    public JsonResult<String> login(@RequestBody UserLoginDTO userLoginDTO){
        String jwt = iUserService.login(userLoginDTO);
        return JsonResult.ok(jwt);
    }

    @GetMapping(value = "/currentUser")
    public JsonResult<UserInfoVO> getUserInfoByNickname(HttpServletRequest request){
        String nickname = (String) request.getAttribute("nickname");
        UserInfoVO userInfo = iUserService.getUserInfoByNickname(nickname);
        return JsonResult.ok(userInfo);
    }

    @PostMapping(value = "/updateUser")
    public JsonResult<Void> updateUser(@RequestBody UserInfoVO userInfoVO){
        iUserService.update(userInfoVO);
        return JsonResult.ok();
    }

    @PostMapping(value = "/updatePassword")
    public JsonResult<Void> updatePassword(@RequestBody UserPasswordVO userPasswordVO){
        iUserService.updatePassword(userPasswordVO);
        return JsonResult.ok();
    }
}
